<?php
/**
 *@author Connor <caokang@foxmail.com>
 *@todo
 *@link www.zeevin.com
 */
class PostController extends BaseController{

	public function actionIndex(){
		$rs = Post::getList();
		$this->render('index',array('data'=>$rs));
	}

	public function actionNew(){
		$model = new Post;
		if(isset($_POST['Post'])){
			$model->attributes=$_POST['Post'];
			if($model->save()){
				$link=CHtml::link(CHtml::encode(mb_substr($model->subject,0,16)), 'javascript:void(0)',array('tag'=>$model->id,'class'=>'article_zee'));
				// echo $link;exit;
				Feed::addfeed(Feed::TYPE_BLOG_MSG, $link);
				$msg['status']=0;
				$msg['desc']='日志添加成功';
			}
		}
		$this->render('new',array('model'=>$model,'msg'=>$msg));
	}

	public function actionView(){

	}

	public function actionEdit(){

		$model=$this->loadRecord();
		if(isset($_POST['Post']))
		{
			$model->attributes=$_POST['Post'];
			if($model->save()){
				$msg['status']=0;
				$msg['desc']='日志修改成功';
			}
		}

		$this->render('edit',array('model'=>$model,'msg'=>$msg));
	}

	public function actionDel(){
		if(Yii::app()->request->isAjaxRequest){
			if(Post::del($_POST['id'])){
				$msg['status']=0;
			}else{
				$msg['status']=1;
				$msg['desc']='日志删除失败！';
			}
			echo json_encode($msg);
		}
	}
	
	private function loadRecord($id = null) {
		if ($this->_rec === null) {
			if ($id !== null || isset ( $_GET ['id'] ))
				$this->_rec = Post::model ()->findbyPk ( $id !== null ? $id : $_GET ['id'] );
			if ($this->_rec === null)
				throw new CHttpException ( 404, 'The requested message does not exist.' );
		}
		return $this->_rec;
	}
}